package com.yinhai.asynctask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Toast;

import com.lz.util.Utils;
import com.yinhai.activity.SystemSetupActivity;
import com.yinhai.cfg.AppConfig;
import com.yinhai.constant.Contact;
import com.yinhai.constant.UrlConstant;
import com.yinhai.map.ServiceMap;
import com.yinhai.receiver.SyncContactService;
import com.yinhai.service.CloudNoteServiceImpl;
import com.yinhai.service.ContactUserServiceImpl;
import com.yinhai.util.CMUtil;
import com.yinhai.util.DateUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.Map;

public class LoginTask extends AsyncTaskForHttpProcess {
	String retStr = null;
	private Context context;
	private CloudNoteServiceImpl cnServiceIpml;
	private ContactUserServiceImpl cusi = null;
	private String userNamePhone="";
	public LoginTask(Context context) {
		super(context, false);
		this.context = context;
	}

	public LoginTask(Context context, boolean autoForword) {
		super(context, autoForword);
	}

	public LoginTask(Context context, int timeoutRequestCount,
			boolean autoForword) {
		super(context, timeoutRequestCount, autoForword);
	}
	public void setUserNamePhone(String userNamePhone) {
		this.userNamePhone = userNamePhone;
	}
	@Override
	protected void parse(Map<String, Object> result) {
		this.retStr = (String) result.get(AsyncTaskContext.RESPONSE_CONTENT);
		JSONObject jo;
		try {
			jo = new JSONObject(retStr);

			if ("ok".equals(jo.getString("status"))) {
				SharedPreferences preferences = context.getSharedPreferences(
						AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
				if(UrlConstant.IS_UPDATE==null) {
					UrlConstant.IS_UPDATE="1";
					try{
			        	 Intent syncContactIntent = new Intent(context, SyncContactService.class);
			        	 syncContactIntent.putExtra("phone", userNamePhone);
			        	 context.startService(syncContactIntent);
			             ServiceMap.getMap().put(Contact.SYNC_CONTACT_SERVICE, syncContactIntent);
			             Editor editor1 = preferences.edit();
			             editor1.putLong("onLineUpdateTime", System.currentTimeMillis());
			             editor1.putString("onLineContactLastUpdateTime",
			                 DateUtil.dateFormatString(new Date(), DateUtil.DAY_TIME_FORMATE));
			             editor1.commit();
			        } catch(Exception e) {
			        	e.printStackTrace();
			        }
				}
				
				Editor editor = preferences.edit();
				editor.putString("name", jo.getString("user_phone"));
				editor.putString("password", jo.getString("user_password"));
				editor.putString("user_code", jo.getString("user_code"));
				editor.putBoolean("isFirstStart", false);
				editor.putBoolean("isLoginSuccess", true);
				editor.putString("im_user_name", jo.getString("im_user_name"));
				editor.putString("im_user_password", jo.optString("im_user_password"));
				editor.putString("im_appkey", jo.optString("im_appkey"));
				editor.putInt("version", 0);
				editor.putInt("version1", 0);
				editor.commit();
				CMUtil.getInstance(context).doLogin();
				cnServiceIpml = new CloudNoteServiceImpl(context);
				cnServiceIpml.init(jo.getString("user_phone"));
				//首次跳到安全设置页面
				Intent mainIntent = new Intent(context, SystemSetupActivity.class);
				mainIntent.putExtra("Main", "1");
				context.startActivity(mainIntent);
				Intent mIntent1 = new Intent("com.gaa.login");
				context.sendBroadcast(mIntent1);
			} else if ("disabled".equals(jo.getString("status"))) {
				cusi = new ContactUserServiceImpl(context);
				SharedPreferences preferences = context.getSharedPreferences(
						AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
				Editor editor = preferences.edit();
				editor.putString("name", "");
				editor.putString("password", "");
				editor.putString("user_code", "");
				editor.putBoolean("isFirstStart", false);
				editor.putBoolean("isLoginSuccess", false);
				editor.putString("im_user_name", "");
				editor.putString("im_user_password", "");
				editor.commit();
				cusi.delContact();
				Utils.SaveIsLogin((Activity) context, 0);
			} else {
				Toast.makeText(context, "注册失败,请联系管理员！", Toast.LENGTH_LONG)
						.show();
				SharedPreferences preferences = context.getSharedPreferences(
						AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
				Editor editor = preferences.edit();
				editor.putBoolean("isFirstStart", true);
				editor.putBoolean("isLoginSuccess", false);
				editor.commit();
				// Toast.makeText(context, "登录失败", Toast.LENGTH_LONG).show();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
